<?php

/*
 * @file Table.php
 * @description 
 * @author Alexis Thou
 * @date 3 sept. 2012
 * 
 */

class Model_TableOrder extends Model {

    private $_name = 'table_order';
    private $_id = 'id';
    private static $_instance;
    
    static function getInstance() {
        if (empty(self::$_instance))
            self::$_instance = new Model_TableOrder();
        return self::$_instance;
    }
    function __construct() {
        parent::__construct($this->_name);
    }

    function selectAvailableTable() {
        $result = $this->_db->query("SELECT tb.id, tb.label, tbo.status FROM TABLES AS tb 
LEFT JOIN table_order AS tbo ON tbo.table_id = tb.id
WHERE ((tb.id = tbo.table_id AND tbo.status != 1) OR (tb.id NOT IN (SELECT table_id FROM table_order))) OR (tb.id = tbo.table_id AND tbo.status = 1) GROUP BY tb.id");

        return $result->fetchAll(PDO::FETCH_OBJ);
    }

    function selectOccupiedTable() {
        $result = $this->_db->query("SELECT tb.id, tb.label, tbo.status FROM TABLES AS tb 
                                    LEFT JOIN table_order AS tbo ON tbo.table_id = tb.id
                                    WHERE (tb.id = tbo.table_id AND tbo.status = 1) GROUP BY tb.id");

        return $result->fetchAll(PDO::FETCH_OBJ);
    }
    
    function getTotalByTid($tid) {

        $result = $this->_db->query('SELECT SUM(price*amount) as total from menu as m 
                           LEFT JOIN `order` as o on  o.menu_id = m.id 
                           WHERE m.id IN(SELECT menu_id as id FROM `order` WHERE table_id = ' . $tid . ') AND table_id='.$tid.'');
        return $result->fetch(PDO::FETCH_OBJ);
    }
    
    /**
     * 
     * @param int $tid
     * @param type $status (1|0)
     */
    function setTableStatus($tid,$status){
        return $this->update(array('status' => $status), array('id' => $tid));
    }
    
    
    function selectPayedTable() {
        $sql = "SELECT *,SUM(price * amount) as total FROM view_order WHERE  `status` =0 AND `date` > '".date("Y-m-d 00:00:00",time()-86400) ."' GROUP BY table_order_id";
        $result = $this->_db->query($sql);
        return $result->fetchAll(PDO::FETCH_OBJ);
    }

    /**
     * 
     * @param int $tid
     * @return type
     */
    function selectCurrentTable($tid) {
        $result = $this->_db->query("SELECT tb.id, tb.label,tbo.id as tableOrderId FROM TABLES AS tb 
                                    LEFT JOIN table_order AS tbo ON tbo.table_id = tb.id
                                    WHERE (tb.id = tbo.table_id AND tbo.status = 1 AND tbo.table_id=$tid)");
        if($result)
            return $result->fetch(PDO::FETCH_OBJ);
        else
            return null;
    }
    
    function set_new_tableOrder($tid) {
        if ($this->check_table_status($tid) == true) {
            $result = $this->select(array("id"),array('table_id'=>$tid,'status'=>1));
            $lastInsertId = $result->id;
        } else {
            $lastInsertId = $this->insert(array('table_id' => $tid, 'date' => date("Y-m-d"), "status" => 1));
        }
        return $lastInsertId;
    }

    function check_table_status($tid) {
        $result = $this->select('count(*) as count', array('table_id' => $tid, 'status' => 1));
        return $result->count > 0 ? true : false;
    }

}

?>
